package com.zhouyu.fileSync.FSclient.transmission.communicationBetweenClients.client;

import com.zhouyu.fileSync.FSclient.ClientAppContext;
import com.zhouyu.fileSync.FScommon.entities.FSClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class CommunicationClientCenter {

    Logger logger = LogManager.getLogger(CommunicationClientCenter.class);
    private final static ExecutorService executorService= Executors.newCachedThreadPool();

    public final static List<CommunicationClient> CommunicationClients =new ArrayList<>();

    public CommunicationClientCenter() {
        buildConnectionToTargetClients();
        startCommunicationSyncJobs();
    }

    private void buildConnectionToTargetClients(){
        //create connection to the target clients
        for (FSClient target : ClientAppContext.CONFIGS.TARGETS) {
            new Thread(){
                @Override
                public void run() {
                    CommunicationClient CommunicationClient = new CommunicationClient(ClientAppContext.CONFIGS.CLIENT, target);
                    CommunicationClients.add(CommunicationClient);
                    executorService.submit(CommunicationClient);
                }
            }.start();
        }

    }


    private void startCommunicationSyncJobs(){
        //create transfer jobSchedule
        CommunicationSyncJobSchedule jobSchedule=new CommunicationSyncJobSchedule();
        executorService.submit(jobSchedule);
    }
}
